package com.cyzy.server.service.impl;

import com.cyzy.common.exception.SelectNullException;
import com.cyzy.pojo.dto.StarDTO;
import com.cyzy.pojo.vo.HuntCardVO;
import com.cyzy.pojo.vo.RecruitCardVO;
import com.cyzy.server.dao.HuntCardDao;
import com.cyzy.server.dao.RecruitCardDao;
import com.cyzy.server.dao.StarDao;
import com.cyzy.server.service.StarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.table.TableRowSorter;
import javax.swing.text.StyledEditorKit;
import java.util.ArrayList;
import java.util.List;

@Service
public class StarServiceImpl implements StarService {
    @Autowired
    private StarDao starDao;

    @Autowired
    private RecruitCardDao recruitCardDao;

    @Autowired
    private HuntCardDao huntCardDao;

    @Override
    @Transactional
    public void starCard(StarDTO starDTO) {
        //帖子类型
        String type = starDTO.getType();
        //帖子id
        Integer cardId = starDTO.getCardId();
        //用户id
        Integer userId = starDTO.getUserId();
        //是否收藏
        Integer isStar = starDTO.getIsStar();

        Boolean flag = false;

        Boolean equal = false;

        List<Integer> userIds = starDao.selectUserIdInStarCard(type);

        if(userIds == null){
            throw new SelectNullException("拉取数据库信息为空");
        }

        //判断表中是否存在这个userId
        for (Integer id : userIds) {
            if (id.equals(userId)) {
                flag = true;
                break; // 跳出循环，不再继续遍历
            }
        }
        //存在该id就执行true的语句块
        if (flag == true) {
            //查询这个userId下是否有这个帖子
            List<Integer> typeCardById = starDao.selectStarTypeCardById(userId, type);
            for (Integer i : typeCardById) {
                if (i.equals(cardId)) {
                    equal = true;
                }
            }
            if (equal == true) {
                //如果这个userId下有这个cardId，就直接更新这个帖子的is_star的值
                starDao.updateStarCard(starDTO);
            } else {
                //不存在这个userId，就插入这个数据
                starDao.insertStarCard(starDTO);
            }
        } else {
            //不存在这个userId，就插入这个数据
            starDao.insertStarCard(starDTO);
        }


    }


    @Override
    public List<RecruitCardVO> selectRecruitStars(int id) {

        List<RecruitCardVO> recruitCardVOS = new ArrayList<RecruitCardVO>();
        //建立暂时索引表，内存对应帖子表的id，由收藏表中查出
        List<Integer> idList = starDao.selectRecruitByUserId(id);
        //对照索引把实体类查出并装入VO与ArrayList
        RecruitCardVO recruitCardVO = new RecruitCardVO();
        for (Integer cardId : idList){
            BeanUtils.copyProperties(recruitCardDao.selectRecruitCardById(cardId),recruitCardVO);
            if (recruitCardVOS.get(0)==null){
                return null;
            }
            recruitCardVOS.add(recruitCardVO);
        }

        return recruitCardVOS;
    }

    @Override
    public List<HuntCardVO> selectHuntStars(int id) {
        List<HuntCardVO> huntCardVOS = new ArrayList<HuntCardVO>();
        //建立暂时索引表，内存对应帖子表的id，由收藏表中查出
        List<Integer> idList = starDao.selectHuntByUserId(id);
        //对照索引把实体类查出并装入VO与ArrayList
        HuntCardVO huntCardVO = new HuntCardVO();
        for (Integer cardId : idList){
            BeanUtils.copyProperties(huntCardDao.selectHuntCardById(cardId),huntCardVO);
            if (huntCardVOS.get(0)==null){
                return null;
            }
            huntCardVOS.add(huntCardVO);
        }

        return huntCardVOS;
    }
}
